#include "Sprite.h"
#include "Game.h"

Sprite::Sprite(LPD3DXSPRITE spriteHandler, Texture* p_texture, int time_animation, int index)
	:p_texture(p_texture)
{
	this->spriteHandler = spriteHandler;

	this->index = 0;
	frame_begin = this->index;
	frame_end = p_texture->getCount();

	this->time_animation = time_animation;
	time_local = 0;

	//temp
	indexFrameTemp = 0;
}

Sprite::~Sprite()
{
	/*if (p_texture)
	delete p_texture;*/
	//p_texture->~Texture();
	//khong nen delete texture, vi no su dung cho nhieu doi tuong khac
}

void Sprite::render(int X, int Y)
{
	RECT srect;

	srect.left = (index % p_texture->getCols()) * p_texture->getWidth() + 1;
	srect.top = (index / p_texture->getCols()) * p_texture->getHeight() + 1;
	srect.right = srect.left + p_texture->getWidth();
	srect.bottom = srect.top + p_texture->getHeight() + 1;

	D3DXVECTOR3 position((float)X, (float)Y, 0);

	this->spriteHandler->Draw(
		p_texture->getTexture(),
		&srect,
		NULL,
		&position,
		D3DCOLOR_XRGB(255, 255, 255));
}

void Sprite::render(float X, float Y, int cameraX, int cameraY)
{
	RECT srect;

	srect.left = (index % p_texture->getCols()) * p_texture->getWidth();
	srect.top = (index / p_texture->getCols()) * p_texture->getHeight();
	srect.right = srect.left + p_texture->getWidth();
	srect.bottom = srect.top + p_texture->getHeight();

	D3DXVECTOR3 position((float)X, (float)Y, 0);
	position.x = X + (float)p_texture->getWidth()/2;
	position.y = Y - (float)p_texture->getHeight()/2;

	//---------------------//
	//D3DXMATRIXA16 matWorld, matTranslate, matScale, rotate;
	//D3DXMatrixTranslation( &matTranslate, -cameraX, 600, 0 );
	//D3DXMatrixTransformation2D(&rotate,NULL,0.0f,NULL,
	//&D3DXVECTOR2(-texture->_Width/2, -texture->_Height/2),D3DXToRadian(0.0f),NULL);
	//D3DXMatrixMultiply(&matTranslate, &matTranslate, &rotate);
	//D3DXMatrixScaling( &matScale, 1.0f, -1.0f, 1.0f );
	////matWorld = matScale * matTranslate;
	//D3DXMatrixMultiply(&matWorld, &matTranslate, &matScale);
	////D3DXMatrixTranslation(&matWorld, -cameraX, cameraY, 0);
	//_spriteHandler->SetTransform(&matWorld);
	// 
	//_spriteHandler->Draw(
	//	texture->Get_Texture(),
	//	&srect,
	//	NULL,
	//	&position,
	//	D3DCOLOR_XRGB(255, 255, 255));
	//-----------------//




	//---------------//
	D3DXMATRIX mt;
	D3DXMatrixIdentity(&mt);
	mt._22 = -1.0f;
	mt._41 = -cameraX;
	mt._42 = 512;//600;

	D3DXVECTOR4 vp_pos;
	D3DXVec3Transform(&vp_pos, &position, &mt);

	D3DXVECTOR3 pos(vp_pos.x, vp_pos.y, 0);
	D3DXVECTOR3 center((float)p_texture->getWidth()/2, (float)p_texture->getHeight()/2, 0);

	this->spriteHandler->Draw(
		p_texture->getTexture(),
		&srect,
		&center,
		&pos,
		D3DCOLOR_XRGB(255, 255, 255)
		);

	//--------------//


	//-------------//
	/*D3DXMATRIXA16 matWorld, matTranslate, matScale, rotate, matRotateX;

	D3DXMatrixIdentity( &matScale );
	D3DXMatrixScaling( &matScale, 1.0f, -1.0f, 1.0f );

	D3DXMatrixIdentity( &matTranslate );
	D3DXMatrixTranslation( &matTranslate, -cameraX, 600, 0 );

	float centerx = texture->_Width / 2;
	float centery = texture->_Height / 2;

	D3DXMatrixIdentity( &rotate );
	D3DXMatrixTransformation2D(&rotate,
	&D3DXVECTOR2(centerx, centery),
	0.0f,
	&D3DXVECTOR2(1,-1),
	NULL,
	D3DXToRadian(0.0f),
	&D3DXVECTOR2(position.x, position.y));

	D3DXMatrixIdentity(&matWorld );
	D3DXMatrixMultiply(&matWorld, &matScale, &matTranslate);
	D3DXMatrixMultiply(&matWorld, &rotate, &matWorld);

	_spriteHandler->SetTransform(&matWorld);

	_spriteHandler->Draw(
	texture->Get_Texture(),
	&srect,
	NULL,
	NULL,
	D3DCOLOR_XRGB(255, 255, 255));

	D3DXMATRIX matDefaut;
	D3DXMatrixInverse(&matDefaut, NULL, &matWorld);
	_spriteHandler->SetTransform(&matDefaut);*/

	//--------------------------//




	//Phien ban 1
	//D3DXMATRIX CameraView;
	//D3DXMatrixTranslation(&CameraView, -cameraX, cameraY, 0);
	//_spriteHandler->SetTransform(&CameraView);
	//_spriteHandler->Draw(
	//	texture->Get_Texture(),
	//	&srect,
	//	NULL,
	//	&position, 
	//	D3DCOLOR_XRGB(255,255,255)
	//);
	//D3DXMATRIX matDefaut;
	//D3DXMatrixInverse(&matDefaut,NULL, &CameraView);
	////D3DXMatrixTransformation2D(&matDefaut,NULL,0.0f,NULL,NULL,0.0f,NULL); 
	//_spriteHandler->SetTransform(&matDefaut);
}

void Sprite::render(float X, float Y, float temp_x, float temp_y, int cameraX, int cameraY)
{
	RECT srect;

	srect.left = (index % p_texture->getCols()) * p_texture->getWidth();
	srect.top = (index / p_texture->getCols()) * p_texture->getHeight();
	srect.right = srect.left + p_texture->getWidth();
	srect.bottom = srect.top + p_texture->getHeight();

	D3DXVECTOR3 position((float)temp_x, (float)temp_y, 0);
	//position.x = X + (float)p_texture->getWidth()/2;
	//position.y = Y - (float)p_texture->getHeight()/2;

	//---------------//
	D3DXMATRIX mt;
	D3DXMatrixIdentity(&mt);
	mt._22 = -1.0f;
	mt._41 = -cameraX;
	mt._42 = 512;

	D3DXVECTOR4 vp_pos;
	D3DXVec3Transform(&vp_pos, &position, &mt);

	D3DXVECTOR3 pos(vp_pos.x, vp_pos.y, 0);
	D3DXVECTOR3 center((float)p_texture->getWidth()/2, (float)p_texture->getHeight()/2, 0);

	this->spriteHandler->Draw(
		p_texture->getTexture(),
		&srect,
		&center,
		&pos,
		D3DCOLOR_XRGB(255, 255, 255)
		);

	//--------------//



}

void Sprite::nextFrame()
{
	index ++;
	if (index < frame_begin)
		index = frame_begin;
	if (index > frame_end - 1)
		index = frame_begin;
}

void Sprite::resetFrame()
{
	index = frame_begin;
	time_local = 0;
}

void Sprite::update(int delta_time)
{
	time_local += delta_time;
	if (time_animation == 0)
	{
	}
	else if (time_local >= time_animation)
	{
		time_local = 0;
		this->nextFrame();
		indexFrameTemp++;
	}
}

void Sprite::setFrame(int frame_begin, int frame_end)
{
	this->frame_begin = frame_begin;
	this->frame_end = frame_end;
}

void Sprite::setIndex(int index)
{
	this->index = index;
}